package com.firmboy.jpa.user.controller;

import com.firmboy.jpa.user.dao.UserRepository;
import com.firmboy.jpa.user.model.User;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.util.List;
import java.util.Optional;

/**
 * user的controller
 *
 * @author firmboy
 * @create 2018-03-12 下午2:33
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/add")
    public String add(){
        User user = new User();
        user.setId("3");
        user.setUserName("tom");
        user.setPassword("123");
        userRepository.save(user);
        return "success";
    }

    @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
    public User findUserById(@PathVariable("id") String id){
        Optional<User> op = userRepository.findById(id);
        User user = op.get();

        return user;
    }


//    @RequestMapping(value = "/test/{id}" ,method = RequestMethod.GET)
//    public User getUserById(@PathVariable("id") String id){
//        User user = userRepository.findOne(id);
//        return user;
//    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<User> getAccounts() {
        return userRepository.findAll();
    }
}
